package com.aube.base.util;

import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.aube.interact.base.R;

/**
 * Created by huyaonan on 16/6/14.
 */
public class AnimatorUtil {

    /**
     * ValueAnimator封装
     * @param duration
     * @param start
     * @param end
     * @param updateListener
     * @param animListener
     */
    public static void animFloat(int duration, float start, float end, ValueAnimator.AnimatorUpdateListener updateListener, AnimatorListenerAdapter animListener) {
        ValueAnimator anim = ValueAnimator.ofFloat(start, end);
        anim.setDuration(duration);
        anim.addUpdateListener(updateListener);
        anim.addListener(animListener);
        anim.start();
    }

    public static void show(final View view) {
        if(view.getVisibility() == View.VISIBLE)
            return;

        Animation anim = AnimationUtils.loadAnimation(view.getContext(), R.anim.scale_in);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        anim.setStartOffset(500);
        anim.setDuration(100);
        view.startAnimation(anim);
    }

    public static void hide(final View view) {
        if(view.getVisibility() == View.GONE)
            return;

        Animation anim = AnimationUtils.loadAnimation(view.getContext(), R.anim.scale_out);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        anim.setDuration(100);
        view.startAnimation(anim);
    }

}
